package com.example.myapplication;


import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;


import org.w3c.dom.Text;

import java.security.PublicKey;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private ViewPager viewPager;
    private int[] mImg;
    private int[] mImg_id;
    private String[] mDec;
    private ArrayList<ImageView> mImgList;
    private LinearLayout ll_dots_container;
    private TextView loop_dec;
    private int previousSelectedPosition = 0;
    boolean isRunning = false;


    ListView listView;



    @Override
    protected  void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        initView();

        initLoopView();



        listView= (ListView)findViewById(R.id.lv);
        List<Gamer> gamesmsg = new ArrayList<>();
        for (int i = 0; i<1 ; i++)
        {
            Gamer nier = new Gamer(R.drawable.nier,"《尼尔：机械纪元》实体收藏版发售");
            gamesmsg.add(nier);
            Gamer mhw = new Gamer(R.drawable.mhw,"《怪物猎人》新DLC或将年底发售");
            gamesmsg.add(mhw);
            Gamer chlh = new Gamer(R.drawable.chlh,"《彩虹六号：围攻》武器调整一览");
            gamesmsg.add(chlh);
            Gamer yjwj = new Gamer(R.drawable.yjwj,"《永劫无间》新赛季一赏（新角色，新武器，新装扮）");
            gamesmsg.add(yjwj);
            Gamer gq = new Gamer(R.drawable.gq,"《鬼泣五》某UP无伤高难通关实录");
            gamesmsg.add(gq);

        }

        GamerAdapter adapter = new GamerAdapter(MainActivity.this,R.layout.game_item,gamesmsg);
        listView.setAdapter(adapter);

    }



    private void initView()
    {
        ImageButton games = findViewById(R.id.games);
        ImageButton msg = findViewById(R.id.msg);
        ImageButton me = findViewById(R.id.me);
        games.setOnClickListener(this);
        msg.setOnClickListener(this);
        me.setOnClickListener(this);
    }

    public void onClick (View v)
    {
        switch (v.getId())
        {
            case R.id.games:
                Intent intent = new Intent(MainActivity.this,Game.class);
                startActivity(intent);
                overridePendingTransition(0,0);
                finish();
                break;
            case R.id.msg:
                Intent intent1 = new Intent(MainActivity.this,Msg.class);
                startActivity(intent1);
                overridePendingTransition(0,0);
                finish();
                break;
            case R.id.me:
                Intent intent2 = new Intent(MainActivity.this,Me.class);
                startActivity(intent2);
                overridePendingTransition(0,0);
                finish();
                break;
        }
    }

    private void initLoopView() {
        viewPager = (ViewPager) findViewById(R.id.loopviewpager);
        ll_dots_container = (LinearLayout) findViewById(R.id.ll_dots_loop);
        loop_dec = (TextView) findViewById(R.id.loop_dec);

        mImg = new int[]
                {
                        R.drawable.yjwj,
                        R.drawable.nier,
                        R.drawable.chlh,
                        R.drawable.gq,
                        R.drawable.mhw
                };

        mDec = new String[]
                {
                        "《永劫无间》新赛季一赏（新角色，新武器，新装扮）",
                        "《尼尔：机械纪元》实体收藏版发售",
                        "《彩虹六号：围攻》武器调整一览",
                        "《鬼泣五》某UP无伤高难通关实录",
                        "《怪物猎人》新DLC或将年底发售"
                };

        mImg_id = new int[]
                {
                        R.id.paper_img1,
                        R.id.paper_img2,
                        R.id.paper_img3,
                        R.id.paper_img4,
                        R.id.paper_img5,
                };

        mImgList = new ArrayList<ImageView>();
        ImageView imageView;
        View dotView;
        LinearLayout.LayoutParams layoutParams;
        for (int i = 0; i < mImg.length; i++) {
            imageView = new ImageView(this);
            imageView.setBackgroundResource(mImg[i]);
            imageView.setId(mImg_id[i]);
            imageView.setOnClickListener(new pagerOnClickListener(getApplicationContext()));
            mImgList.add(imageView);

            dotView = new View(this);
            dotView.setBackgroundResource(R.drawable.dot);
            layoutParams = new LinearLayout.LayoutParams(10, 10);
            if (i != 0) {
                layoutParams.leftMargin = 10;
            }

            dotView.setEnabled(false);
            ll_dots_container.addView(dotView, layoutParams);
        }

        ll_dots_container.getChildAt(0).setEnabled(true);
        loop_dec.setText(mDec[0]);
        previousSelectedPosition = 0;

        viewPager.setAdapter(new LoopViewAdapter(mImgList));
        int m = (Integer.MAX_VALUE / 2) % mImgList.size();
        int currentPosition = Integer.MAX_VALUE / 2 - m;
        viewPager.setCurrentItem(currentPosition);

        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                int newPosition = i % mImgList.size();
                loop_dec.setText(mDec[newPosition]);
                ll_dots_container.getChildAt(previousSelectedPosition).setEnabled(false);
                ll_dots_container.getChildAt(newPosition).setEnabled(true);
                previousSelectedPosition = newPosition;

            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });


        new Thread()
        {
            public void run()
            {
                isRunning = true;
                while (isRunning)
                {
                    try{
                        Thread.sleep(5000);
                    }catch (InterruptedException e){
                        e.printStackTrace();
                    }

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            viewPager.setCurrentItem(viewPager.getCurrentItem()+1);
                        }
                    });
                }
            }
        }.start();

    }


}